package com.firewolf.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class MyRealm2 extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = "liu"; //根据账号去数据库查询密码等信息
        String salt2 = username;
        
        //数据库取出密码：这个算法就是往数据库里面存的时候用的算法,用到的这个盐值可以存放在数据库里面
        String password = "234"; //加密后的密码
        SimpleHash simpleHash = new SimpleHash("MD5", password, salt2, 2);
        String enCodePassword = simpleHash.toString();
        
        SimpleAuthenticationInfo ai = new SimpleAuthenticationInfo(username, enCodePassword, getName());
        //设置盐，这个盐可以保存在数据库中
        ai.setCredentialsSalt(ByteSource.Util.bytes(salt2)); //盐是用户名+随机数
        
        
        return ai;
    }
}